import {computed, onUnmounted, ref} from 'vue'
import dayjs from 'dayjs'
//封装倒计时逻辑函数
export const useCountDown = ()=>{
    let timer = null
    //响应式数据
    const time = ref(0)
    const formatTime = computed(()=>dayjs.unix(time.value).format('mm分ss秒'))
    //开始倒计时方法
    const start = (currentTime)=>{
        time.value = currentTime
        //开始倒计时逻辑
        timer = setInterval(()=>{
            time.value--;
        },1000)
    }
    //组件销毁时清除定时器
    onUnmounted(()=>{
        timer&& clearInterval(timer)
    })
    return {
        formatTime,
        start
    }
}